package net.sunbeta.test.dbunit;

import java.net.URL;
import java.sql.SQLException;

import org.dbunit.JdbcBasedDBTestCase;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.junit.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class DBunitDemo extends JdbcBasedDBTestCase {

	@Override
	protected String getConnectionUrl() {
		return "jdbc:postgresql://127.0.0.1/userdb";
	}

	@Override
	protected String getDriverClass() {
		return "org.postgresql.Driver";
	}

	@Override
	protected String getPassword() {
		return "pass";
	}

	@Override
	protected String getUsername() {
		return "username";
	}

	@Override
	protected IDataSet getDataSet() throws Exception {
		URL url = getClass().getResource("/user-roles.xml");
		return new FlatXmlDataSet(url);
	}

	@BeforeMethod
	protected void setUp() throws Exception {
		super.setUp();
	}

	@AfterMethod
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	@Test
	public void verifyRemove() throws SQLException, Exception {
		assert getConnection().getRowCount("USERS") == 2;
		
		String whereClause = "user_id = 1";
		assert getConnection().getRowCount("USER_ROLES", whereClause) == 0;
	}
	
	@Test
	public void verifyUserModification() throws Exception {
		
		URL url = getClass().getResource("/user-roles-expected.xml");
		IDataSet data = new FlatXmlDataSet(url);
		ITable expected = data.getTable("USERS");
		
		// we have the expected data,
		// next we perform operations on the db
		// code that results in USERS table changes...
		// check that our modifications match our expectations
		IDataSet dbSet = getConnection().createDataSet();
		assertEquals(expected, dbSet.getTable("USERS"));
	}
}
